#include "DbStorage/DbStorageFactory.hpp"
#include "DbStorage/ProjectDbStorage.hpp"
#include "DbStorage/PackageDbStorage.hpp"
#include "DbStorage/RequirementsDbStorage.hpp"
#include "DbStorage/CreateProjectStorageCmd.hpp"
#include "DbStorage/CreatePackageStorageCmd.hpp"
#include "DbStorage/RemovePackageStorageCmd.hpp"
#include "DbStorage/CreateRootRequirementStorageCmd.hpp"
#include "DbStorage/RemoveRootRequirementStorageCmd.hpp"
#include "CaseDB/Connection.hpp"

namespace DbStorage
{
	

DbStorageFactory::DbStorageFactory( CaseDB::Connection* pDbConnection)
	: pProjectStorage_( NULL)
	, pPackageStorage_( NULL)
	, pDbConnection_( pDbConnection)
{
	std::auto_ptr< ProjectDbStorage> pPrjStorage( new ProjectDbStorage( *this, pDbConnection_));
	std::auto_ptr< PackageDbStorage> pPackStorage( new PackageDbStorage( *this, pDbConnection_));
	std::auto_ptr< RequirementsDbStorage> pReqStorage( new RequirementsDbStorage( *this, pDbConnection_));
	pProjectStorage_ = pPrjStorage.release();
	pPackageStorage_ = pPackStorage.release();
	pRequirementStorage_ = pReqStorage.release();
	pDbConnection_->open();
}

// ---------------------------------------------------------------------------------------------------------------------

DbStorageFactory::~DbStorageFactory()
{
	delete pProjectStorage_;
	delete pPackageStorage_;
}

// ---------------------------------------------------------------------------------------------------------------------

Storage::PackageStorage& DbStorageFactory::getPackageStorage()
{
	return *pPackageStorage_;
}

// ---------------------------------------------------------------------------------------------------------------------

Storage::ProjectStorage& DbStorageFactory::getProjectStorage()
{
	return *pProjectStorage_;
}

// ---------------------------------------------------------------------------------------------------------------------

Storage::RequirementsStorage& DbStorageFactory::getRequirementStorage()
{
	return *pRequirementStorage_;
}

// ---------------------------------------------------------------------------------------------------------------------

Storage::StorageCommand* DbStorageFactory::createProjectCreationCommand( Model::Project& subject )
{
	return new CreateProjectStorageCmd( subject, this->dbConnection());
}

// ---------------------------------------------------------------------------------------------------------------------

Storage::StorageCommand* DbStorageFactory::createPackageCreationCommand( Model::Package& subject )
{
	return new CreatePackageStorageCmd( subject, this->dbConnection());
}

// ---------------------------------------------------------------------------------------------------------------------

Storage::StorageCommand* DbStorageFactory::createPackageRemovalCommand( Model::Package& subject )
{
	return new RemovePackageStorageCmd( subject, this->dbConnection());
}

// ---------------------------------------------------------------------------------------------------------------------

Storage::StorageCommand* DbStorageFactory::createRootRequirementCreationCommand( Model::Requirement& subject)
{
	return new CreateRootRequirementStorageCmd( subject, this->dbConnection());
}

// ---------------------------------------------------------------------------------------------------------------------

Storage::StorageCommand* DbStorageFactory::createRootRequirementRemovalCommand( Model::Requirement& subject, Model::Package* pParentPackage )
{
	return new RemoveRootRequirementStorageCmd( subject, pParentPackage, this->dbConnection());
}

} // namespace DbStorage
